using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;
using NTTHAO.Imaging;

public partial class admin_EditorFileUploader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFile file = Request.Files[0];
                bool addLayerMask = Request.Params["addLayerMask"] == null ? false : "1".Equals(Request.Params["addLayerMask"]);
                string path = "/";
                if (Request.Params["path"] != null)
                {
                    path = Request.Params["path"].Trim();
                    path = path.StartsWith("/") ? path.Remove(0, 1) : path;
                    path = (path.Length < 1 || path.EndsWith("/")) ? path : (path + "/");
                }

                string absolutePath = "/" + Constant.UPLOAD_FILE_PATH + "Files/" + path;
                if (!Directory.Exists(Server.MapPath(".." + absolutePath)))
                {
                    Directory.CreateDirectory(Server.MapPath(".." + absolutePath));
                }

                int index = 1;
                string filePath = absolutePath + Path.GetFileName(file.FileName);
                while (File.Exists(Server.MapPath(".." + filePath)))
                {
                    index++;
                    filePath = absolutePath + Path.GetFileNameWithoutExtension(file.FileName) + "_" + index.ToString() + Path.GetExtension(file.FileName);
                }

                string mappedPath = Server.MapPath(".." + filePath);
                file.SaveAs(mappedPath);

                string fileEx = Path.GetExtension(mappedPath);
                if (addLayerMask && ".jpg".Equals(fileEx, StringComparison.OrdinalIgnoreCase))
                {
                    this.AddLayerMask(mappedPath);
                }

                Response.ClearContent();
                Response.HeaderEncoding = System.Text.Encoding.UTF8;
                Response.Write(filePath);
            }
        }
        catch { }
    }

    protected void AddLayerMask(string serverFileName)
    {
        //layer mask
        if (Constant.ADD_LAYER_MASK)
        {
            float left = 0, top = 0, width = 0, height = 0, opacity = 1;
            float.TryParse(ConfigValues.GetConfigValue("MASK_TOP_PERCENT").Trim(), out top);
            float.TryParse(ConfigValues.GetConfigValue("MASK_LEFT_PERCENT").Trim(), out left);
            float.TryParse(ConfigValues.GetConfigValue("MASK_WIDTH_PERCENT").Trim(), out width);
            float.TryParse(ConfigValues.GetConfigValue("MASK_HEIGHT_PERCENT").Trim(), out height);
            float.TryParse(ConfigValues.GetConfigValue("MASK_OPACITY").Trim(), out opacity);

            ImageUtilities.DrawLayerMask(serverFileName, Server.MapPath("../" + Constant.MASK_IMAGE_FILE), top, left, width, height, ResizeOptions.MaximumSizeFix, opacity, ImageUtilities.HIGHT_PROGRESSIVE);
        }
        // end
    }
}
